<?php
class Merchant_Model_QualityControlItem
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_QualityControlItem();
	}
	
	public static function getInstance(){
		return new Merchant_Model_QualityControlItem();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row quality_control_item data 
	*@return boolean
	*/
	public  function add($row){	 
		$row['qci_create_time'] = date("Y-m-d H:i:s");
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row quality_control_item data 
	*@param int $qci_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$row['qci_last_update'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto('qci_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
		
	public function updateByQcoCodeAndProductId($row,$qco_code,$product_id){
		$where = $this->_table->getAdapter()->quoteInto('qco_code = ?', $qco_code);
		$where .= $this->_table->getAdapter()->quoteInto(' and product_id = ?', $product_id);
		return $this->_table->update($row, $where);
	}
	/**
	*No Delete One Row
	*@param int $qci_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('qci_id = ?', $id); 
		return $this->_table->delete($where);
	}
	/*
	*根据qco_code删除
	*/
	 public function deleteByQcoCode($qco_code){
		$where = $this->_table->getAdapter()->quoteInto('qco_code = ?', $qco_code); 
		return $this->_table->delete($where);
	}
	
	/**
	* 
	* @param int $qco_code
	* @return zend_db_table_row
	*/
	public function getQcoCodeAll($qco_code){
		$where = $this->_table->getAdapter()->quoteInto("qco_code = ?",$qco_code);
		return $this->_table->fetchAll($where);
	}
	
	public function getByQcoCode($qco_code){
		$where = $this->_table->getAdapter()->quoteInto("qco_code = ?",$qco_code);
		return $this->_table->fetchRow($where);
	}
	/**
	* @param string $qco_code and int $product_id
	* @return zend_db_table_row
	*/
	public function getByQcoCodeAndProductId($qco_code,$product_id){
		$where = $this->_table->getAdapter()->quoteInto("qco_code = ?",$qco_code);
		$where.= $this->_table->getAdapter()->quoteInto(" and product_id= ?", $product_id);
		return $this->_table->fetchRow($where);
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby="qci_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["qco_code"]) && $condition["qco_code"]!=''){
             $select->where(" qco_code = ?",$condition["qco_code"]);
        }
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["qci_status"]) && $condition["qci_status"]!=''){
            $select->where(" qci_status = ?",$condition["qci_status"]);
        }
		if(isset($condition["user_id"]) && $condition["user_id"]!=''){
            $select->where(" user_id = ?",$condition["user_id"]);
        }
		if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
            $select->where(" ws_code = ?",$condition["ws_code"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
			
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}	
}